#ifndef _BASE_H
#define _BASE_H

//文件块大小
#define BLOCK_SIZE 64

//消息类型
#define C2S_FILENAME 1
#define S2C_FILEINFO 2
#define C2S_LOADINFO 3
#define S2C_FILEBLOCK 4


struct C2S_filename{
    int type;
    char filename[128];
};

struct S2C_fileinfo{
    int type;
    size_t size; 
};

struct C2S_loadinfo{
    int type;
    char filepath[128];
    off_t offset;
    size_t block_size;
};
struct S2C_file_block{
    int type;
    off_t offset;
    unsigned char content[BLOCK_SIZE];
};

//创建动态内存buftosend
void* create_buftosend(int size);

//释放buftosend的内存
void free_buftosend(char** buftosend);

//对 struct C2S_filename 做序列化,返回序列化后的buffer大小
int serialize_filename(char* buffer,const struct C2S_filename* msg);

//对 struct S2C_fileinfo 做序列化,返回序列化后的buffer大小
int serialize_fileinfo(char* buffer,const struct S2C_fileinfo* msg);

//对 struct C2S_loadinfo 做序列化,返回序列化后的buffer大小
int serialize_loadinfo(char* buffer,const struct C2S_loadinfo* msg);

//对 struct S2C_file_block 做序列化,返回序列化后的buffer大小
int serialize_fileblock(char* buffer,const struct S2C_file_block* msg,int n);

//对 struct C2S_filename 做反序列化
int unserialize_filename(struct C2S_filename* msg,const char* buffer);

//对 struct S2C_fileinfo 做反序列化
int unserialize_fileinfo(struct S2C_fileinfo* msg,const char* buffer);

//对 struct C2S_loadinfo 做反序列化
int unserialize_loadinfo(struct C2S_loadinfo* msg,const char* buffer);

//对 struct S2C_file_block 做反序列化
int unserialize_fileblock(struct S2C_file_block* msg,const char* buffer);

//解决粘包,返回是否成功,成功返回0,失败返回-1
char* addlength(const char* buffer,int size);

//接受消息头,返回消息大小
int recv_header(int sockfd);
#endif